/**
 * 
 */
package com.gamutsoft.amb.common.util;

import java.util.Locale;
import java.util.ResourceBundle;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author liepingzhu
 *
 */
public class MessageBundleUtil {
	 static String I18NNAME="globalMessages";//国际化资源文件的前缀名

	/**
	 * 根据Message Key获得当前国际化后的消息信息
	 * （从资源文件中获取）
	 * 
	 * @param key
	 * @param params
	 * @return
	 */
	public static String getMessage(String key, String...params) {
		String result = "";
		
		// 获得配置文件设定
		//String srcName = "globalMessages";//DefaultSettings.get("struts.custom.i18n.resources");
		
		// 获得Struts的目前的语言设定
		ActionContext actionCtx = ActionContext.getContext();
		Locale local = actionCtx.getLocale();
		
		//ResourceBundle bundle = ResourceBundle.getBundle(srcName, local);
		ResourceBundle bundle = ResourceBundle.getBundle(I18NNAME, local);
		
		try {
			result = bundle.getString(key);
		} catch (Exception e) {
			result = key;
		}
		return result;
	}
	
	/**
	 * 获得当前语言代码
	 * 
	 * @return
	 */
	public static String getRequestLocal() {
		ActionContext actionCtx = ActionContext.getContext();
		Locale local = null;
		try{
			local = actionCtx.getLocale();
		}catch(Exception e){
			local = new Locale("en", "US");
		}
		return local.toString();
	}
}
